Nested If/Then with Eddie

level: beginner

by Alyce Watson [http://alycesrestaurant.com/]

Home

LB Wire Frame Lib

Stylebits - Listboxes

IExpress

Prompt

Icons

Screen Capture

Eddie's Lessons

Combobox

Scrolling Texteditor

Nested If/Then

Sprite Byte

Eddie

Moving Listbox Items

Newsletter help

Submissions

Index


IF THEN ELSE END IF

Liberty BASIC allows us to use conditional statements. This allows us to evaluate an expression and perform code routines according to the condition.

IF THEN

A single line conditional statement looks like this:

IF (expression) THEN 'do code

That statement says, "If the expression evaluates to TRUE, then perform the code routine that follows." A simple, working example looks like this:

x = 2
if (x < 4) then notice "x is less than four."

The expression (x < 4) evaluates to TRUE because x is equal to two and two is less than four.

IF THEN ELSE

We can add the optional ELSE statement so that we can perform an action if the expression does not evaluate to TRUE. A single line conditional statement containing ELSE looks like this:

IF (expression) THEN 'do code ELSE 'do alternate code

Here is an example that is similar to the previous example.

x = 5
if (x < 4) then notice "x less than 4" else notice "x not less than 4."

You can also include more than one instruction after the THEN statement, but instructions must be separated by colons. Here is an example:

x = 2
if (x < 4) then notice "x is less than 4" : exit sub

Multi-part Expressions

The expression that is to be evaluated can have many parts. All that is necessary is that the expression evaluate to true or false. Group each part of the larger expression in pairs of parenthesis, like this:

x = 2
if (x < 4) and (x > 0) then notice "Valid Number"

Block Conditional Statements

Unless the conditional statement and code are quite brief, it's best to use the block-if construction. It looks like this:

IF (expression) THEN
    'code to perform if expression is true
ELSE
    'code to perform if expression is false
END IF

The code blocks within each section of the block-if can be many lines, and can call subs, gosubs and functions. Here is an example of a block-if.

x = 2 : y = 13
if (x < 4) and (y > 10) then
    z = x + y
    print "x is ";x
    print "y is ";y
    print "z is ";z
else
    z = 0
    print "Invalid inputs for x and y."
    gosub [failure]
end if

No "ELSEIF" -- use nesting instead.

Some languages have an "ELSEIF" statement that allows us to evaluate any number of expressions in a single block-if. Liberty BASIC does not have this capability, but there are two alternatives. One way to evaluate many conditions is to use SELECT CASE . In Eddie, we've chosen to nest our block-if statements. Here is a simple example of nested IF statements.

x = 2 : y = 13
if (x < 4) then
    if (y > 10) then
        print "Both inputs are valid."
    else
        print "x is valid, y is invalid."
    end if
else
    if (y > 10) then
        print "y is valid, x is invalid."
    else
        print "both inputs are invalid."
    end if
end if

Code Formatting

To keep track of which "ELSE" goes with an "IF" when nesting these statements, it is standard to use tabs to indent the different levels of nesting, as we did in the example above.

Eddie's Nest

In Eddie, we check the ini file to see if it exists by opening for append and checking the length. If it exists, we open for input and read the information, such as the path to liberty.exe. If it does not exist, we have another set of conditional statements. We need to ask the user to find the path to liberty.exe. If he clicks on liberty.exe in the filedialog, we do one thing, but if he cancels the filedialog, we do another thing in the code. Here is the routine in Eddie that has nested IF statements:

[GetLibertyPath]
    timer 0
    'file to store path info is IniFile$
    'open for append to see if file exists
    'attempting to open a nonexistent file for INPUT causes an error
    open IniFile$ for append as #f
    lenFile = LOF(#f)
    close #f

    'if the length = 0, the file doesn't exist
    if lenFile=0 then
        filedialog "Find Liberty.exe","*liberty*.exe",LibertyExe$
        if LibertyExe$="" then
            'the user cancelled the filedialog
            notice "Liberty.exe not found."
        else
            'the user selected liberty.exe on his computer
            'open the ini file and store the information
            open IniFile$ for output as #f
            print #f, LibertyExe$
            close #f
        end if
    else
        'if the length is greater than 0, the file exists
        'open it for input and read the first line
        'into the variable LibertyExe$
        open IniFile$ for input as #f
        line input #f, LibertyExe$
        close #f
    end if
    TIMER 50, [activateTimer]
    RETURN          

DEMO

For the Eddie code, go here: Eddie


Home

LB Wire Frame Lib

Stylebits - Listboxes

IExpress

Prompt

Icons

Screen Capture

Eddie's Lessons

Combobox

Scrolling Texteditor

Nested If/Then

Sprite Byte

Eddie

Moving Listbox Items

Newsletter help

Submissions

Index